package com.benio.ycfdisplayer.displayer;

import android.content.Context;
import android.view.View;

import com.benio.ycfdisplayer.fresco.FrescoInitializer;
import com.benio.ycfdisplayer.fresco.photoview.PhotoViewExecutor;
import com.benio.ycfdisplayer.fresco.photoview.TargetBuilder;

/**
 * 对外公开的api
 * Created by benio on 2016/10/9.
 */
public class YcfDisplayer implements Displayer {
    private volatile static YcfDisplayer sDisplayer;
    private Executor mExecutor;
    private TargetFactory mTargetFactory;

    public static YcfDisplayer get() {
        if (sDisplayer == null) {
            throw new IllegalStateException("YcfDisplayer must initialize before using");
        }
        return sDisplayer;
    }

    public static void initialize(Context context) {
        // default implement
        initialize(context, new FrescoInitializer(), new PhotoViewExecutor(), new TargetBuilder());
    }

    public static void initialize(Context context, Initializer initializer, Executor executor, TargetFactory targetFactory) {
        if (sDisplayer == null) {
            if (initializer == null) {
                throw new NullPointerException("Initializer cannot be null");
            }
            initializer.initialize(context);
            sDisplayer = new YcfDisplayer(executor, targetFactory);
        }
    }

    private YcfDisplayer(Executor executor, TargetFactory targetFactory) {
        if (executor == null) {
            throw new NullPointerException("Executor cannot be null");
        }
        if (targetFactory == null) {
            throw new NullPointerException("TargetFactory cannot be null");
        }
        this.mExecutor = executor;
        this.mTargetFactory = targetFactory;
    }

    public TargetFactory getTargetFactory() {
        return mTargetFactory;
    }

    public Executor getExecutor() {
        return mExecutor;
    }

    public RequestBuilder display(String uri) {
        return GenericRequestBuilder.obtain(mExecutor, mTargetFactory).uri(uri);
    }

    public void display(String uri, View view) {
        display(uri).into(view);
    }

    @Override
    public void display(String uri, Target target) {
        display(uri).into(target);
    }
}
